// ALXEditCtrl.cpp : implementation file
//

#include "stdafx.h"
#include "AdvButton.h"
#include "resource.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAdvButton

CAdvButton::CAdvButton()
{
}

CAdvButton::~CAdvButton()
{
}


BEGIN_MESSAGE_MAP(CAdvButton, CButton)
	//{{AFX_MSG_MAP(CAdvButton)
	ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
	ON_WM_KILLFOCUS()
	ON_WM_KEYDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAdvButton message handlers

BOOL CAdvButton::ActivateCtrl(int x, int y, int cx, int cy)
{
	MoveWindow(max(x,x+(cx-cy)),y+1,min(cx,cy),cy-1);
	EnableWindow(TRUE);
	ShowWindow(SW_SHOW);
	CButton::SetFocus();
	return TRUE;
}

BOOL CAdvButton::DeactivateCtrl()
{
	EnableWindow(FALSE);
	ShowWindow(SW_HIDE);
	MoveWindow(0,0,0,0);
	return TRUE;
}

void CAdvButton::SetData(CELL_DATA CtrlData)
{
	m_Data = CtrlData;
	return;
}

CELL_DATA CAdvButton::GetCellData()
{
	return m_Data;
}

BOOL CAdvButton::IsActive()
{
	return IsWindowEnabled();
}

BOOL CAdvButton::DestroyCtrl()
{
	return DestroyWindow();
}

BOOL CAdvButton::CreateCtrl(DWORD dwStyle, CWnd *pParentWnd, UINT nID)
{
	CRect rectCtrl(0,0,0,0);
	return	Create("",dwStyle,rectCtrl,pParentWnd,nID);
}

void CAdvButton::SetFontCtrl(CFont *pFont, BOOL bRedraw)
{
	SetFont(pFont,bRedraw);
}

BOOL CAdvButton::EnableCtrl(BOOL bEnable)
{
	return	EnableWindow(bEnable);
}
BOOL CAdvButton::PreTranslateMessage(MSG* pMsg) 
{
	if(pMsg->message==WM_KEYDOWN)
	{
		switch (LOWORD(pMsg->wParam))
		{
			case VK_DOWN: case VK_UP: case VK_HOME: case VK_END: case VK_PRIOR: case VK_NEXT: case VK_LEFT: case VK_RIGHT: case VK_RETURN: case VK_TAB:
				CAdvButton::GetParent()->PostMessage(WM_KEYDOWN,MAKEWPARAM(LOWORD(pMsg->wParam),0),pMsg->lParam);
				return TRUE;
			default:
				return CButton::PreTranslateMessage(pMsg);
		}
	}
	else
		return CButton::PreTranslateMessage(pMsg);
	
	return CButton::PreTranslateMessage(pMsg);
}

void CAdvButton::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
{
	CDC DC;
	DC.Attach(lpDrawItemStruct->hDC);
	if(lpDrawItemStruct->itemState & ODS_SELECTED)
		DC.DrawFrameControl(&lpDrawItemStruct->rcItem,DFC_BUTTON,DFCS_BUTTONPUSH | DFCS_PUSHED);
	else
		DC.DrawFrameControl(&lpDrawItemStruct->rcItem,DFC_BUTTON,DFCS_BUTTONPUSH);
	int OldMode = DC.GetBkMode();
	DC.SetBkMode(TRANSPARENT); 
	DC.DrawText("...", &lpDrawItemStruct->rcItem, DT_CENTER | DT_VCENTER);
	DC.SetBkMode(OldMode); 
}

void CAdvButton::OnClicked() 
{
	GetParent()->PostMessage(WM_COMMAND,IDC_ADVBUTTON,0);
}

void CAdvButton::OnKillFocus(CWnd* pNewWnd) 
{
	CButton::OnKillFocus(pNewWnd);
	
	CWnd* pWnd = GetParent();
	if(pWnd != pNewWnd && pWnd != NULL)
		if(pNewWnd != NULL)
			pWnd->SendMessage(WM_KILLFOCUS, (WPARAM)pNewWnd->m_hWnd,0);
		else
			pWnd->SendMessage(WM_KILLFOCUS, NULL,0);

}


void CAdvButton::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	if(nChar = VK_F3)
		GetParent()->PostMessage(WM_COMMAND,IDC_ADVBUTTON,0);
	else
		CButton::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CAdvButton::SendLButtonDown(UINT nFlags, CPoint point)
{
	this->PostMessage(WM_LBUTTONDOWN,nFlags,MAKELPARAM(point.x,point.y));
}
